@extends('admin::layouts.master')
@section('title')
    {{$title}}
@endsection
@css(css/goods.css)
@section('tab')
    <form class="layui-form">
    <div class="goods_fax">
        <div class="layui-row">
            <div class="layui-tab layui-tab-brief" lay-filter="filter">
                <ul class="layui-tab-title">
                    <li class="layui-this">商品信息</li>
                    <li id="specsShow">规格属性</li>
                    <li id="img_info">商品相册</li>
                </ul>
                <div class="layui-tab-content">
                    {{--商品信息开始--}}
                    <div class="layui-tab-item layui-show">

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>商品名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="goods[name]" placeholder="商品名称" lay-verify="required" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>副标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="goods[title]" placeholder="副标题" lay-verify="required" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>是否上架</label>
                            <div class="layui-input-block">
                                <select name="goods[status]" class="goods_cate" lay-verify="required">
                                    <option value="">请选择</option>
                                    <option value="1">上架</option>
                                    <option value="2">下架</option>
                                </select>
                            </div>
                        </div>

                        {{--<div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">保养分类</label>
                            <div class="layui-inline" style="width: 29%">
                                <select name="goods[maintain_pid]" lay-filter="maintain_father">
                                    <option value="">请选择一级分类</option>
                                    @foreach($maintain as $key=>$value)
                                        <option value="{{$value['id']}}">{{$value['name']}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="layui-inline" style="width: 30%">
                                <select name="goods[maintain_id]" id="maintain_son">
                                    <option value="">请选择二级分类</option>
                                </select>
                            </div>
                        </div>--}}

                        <div class="layui-form-item">
                            <label class="layui-form-label"><span class="x-red">*</span>服务标签</label>
                            <div class="layui-input-block" id="tags" data-tags="{{$sale_tag}}"></div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">是否包邮</label>
                            <div class="layui-input-block">
                                <input type="radio" name="goods[is_free]" value="1" title="不包邮费" checked>
                                <input type="radio" name="goods[is_free]" value="2" title="包邮费">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">是否推荐</label>
                            <div class="layui-input-block">
                                <input type="radio" name="goods[is_recommend]" value="0" title="不推荐" checked >
                                <input type="radio" name="goods[is_recommend]" value="1" title="推荐">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">商品类型</label>
                            <div class="layui-input-block">
                                <input type="radio" name="goods[type]" lay-filter="goodsCate" value="1" title="普通商品" checked="">
                                <input type="radio" name="goods[type]" lay-filter="goodsCate" value="2" title="积分商品">
                                {{--<input type="radio" name="goods[type]" value="3" title="活动商品">--}}
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>商城分类</label>
                            <div class="layui-input-block">
                                <input type="hidden" value="{{json_encode($cate)}}" id="cateJson">
                                <select name="goods[cate_id]" class="goods_cate" lay-verify="required" id="optionCate">
                                    <option value="">选择商城分类</option>
                                    @foreach($cate['ordinary'] as $k => $v)
                                        <option value="{{$v['id']}}" @if($search['type']==$v['id']) selected @endif>{{$v['name']}}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>商品图片</label>
                            <div class="layui-input-block">
                                <span class="pic_left">
                                    <input type="text" name="goods[pic_url]" value="" lay-verify="required" class="layui-input" readonly="readonly" id="img_url" placeholder="请长传图片大小为800x800，格式为jpg或者png">
                                </span>
                                <span class="pic_right">
                                    <button type="button" class="layui-btn layui-btn-xs layui-btn-normal upimg">上传</button>
                                    <img class="show_img" style="" src="admin/images/upload.png">
                                    <img class="layui-upload-img is_show" src="admin/images/loading.gif">
                                </span>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>展示价格</label>
                            <div class="layui-input-block">
                                <input type="text" name="goods[price]" placeholder="展示价格" lay-verify="required|number" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>价格说明</label>
                            <div class="layui-input-block">
                                <textarea name="goods[price_indication]" placeholder="请输入内容" lay-verify="required" class="layui-textarea"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>售后服务</label>
                            <div class="layui-input-block">
                                <textarea name="goods[sale]" placeholder="请输入内容" lay-verify="required" class="layui-textarea"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>图文详情</label>
                            <div class="layui-input-block">
                                {!! baidu_ueditor('', 'goods[detail]') !!}
                            </div>
                        </div>

                    </div>
                    {{--商品信息结束--}}

                    {{--规格属性开始--}}
                    <div class="layui-tab-item">

                        <div class="layui-form-item">
                            <label for="L_alias" class="layui-form-label">
                                <span class="x-red">*</span>规格分类</label>
                            <div class="layui-input-block"style="width:200px">
                                <select name="specs_cate_id" class="goods_cate" lay-verify="specs_cate">
                                    <option value="">选择规格</option>
                                    @foreach($specs as $ky => $vl)
                                        <option value="{{$vl['id']}}" @if($search['type']==$vl['id']) selected @endif>{{$vl['name']}}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <span>
                            <div class="layui-form-item">
                                <div class="add_specs"></div>
                            </div>
                        </span>
                    </div>
                    {{--规格属性结束--}}

                    {{--商品相册开始--}}
                    <div class="layui-tab-item">
                        <div class="layui-upload">
                            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;border-width: 1px">
                                <div class="layui-upload" lay-verify="img_num" id="picNum">
                                    <span id="imgLst"></span>
                                </div>
                            </blockquote>
                            <button type="button" class="layui-btn" id="more_img">选择图片</button>
                        </div>
                    </div>
                    {{--商品相册结束--}}
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                    </div>

                </div>
            </div>
        </div>
    </div>
        <div class="layui-footer">
            <div class="layui-main">
                <button class="layui-btn" lay-filter="add" lay-submit="">提交</button>
            </div>
        </div>
    </form>
@endsection

@section('tab_js')
    <script src="/js/selectM.js" type="text/javascript" charset="utf-8"></script>
    @include('admin::goods.this_js')
    <script>
        layui.use(['form', 'layer', 'jquery'], function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            var selectM = layui.selectM;
            var tagData = $('#tags').data('tags');

            selectM({
                elem: '#tags'//元素容器【必填】
                , data: tagData//候选数据【必填】
                , name: 'sale_tag'
                , verify: 'required'//添加验证
            });

            $('dd').css('padding', '3px 10px');
            form.render();

            form.verify({
                specs_cate : function(value) {
                    if (value.length == '') {
                        $("#specsShow").trigger("click");
                        return '请选择规格';
                    }
                },
                goods_pic : function(value) {
                    if (value.length == '') {
                        $("#specsShow").trigger("click");
                        return '规格属性图片不能为空';
                    }
                },
                img_num : function(value) {
                    var num = $("#picNum").children("span").length;
                    if(num <= 1){
                        $("#img_info").trigger("click");
                        return '请上传商品详情图';
                    }
                }
            });

            form.on('radio(goodsCate)', function(data){
                var html = goodsCate(data.value);

                $("#optionCate").html(html);
                form.render('select');

                if(data.value == 2){
                    $('.integral input').attr('lay-verify','required');
                    $('.integral').show();
                }else{
                    $('.integral input').attr('lay-verify','');
                    $('.integral').hide();
                }
            });

            function goodsCate(val){
                var option = JSON.parse($('#cateJson').val());
                if(val == 1){
                    var typearray = option.ordinary;
                }else{
                    var typearray = option.integral;
                }

                var html = '<option value="">选择分类</option>';
                $.each(typearray,function(i,n){
                    html += '<option value="'+n.id+'">'+n.name+'</option>';
                });

                return html;
            }

            //监听提交
            form.on('submit(add)', function (data) {
                $.ajax({
                    url: js_url('admin-goods-store'),
                    type: 'post',
                    data: data.field,
                    dataType: "json",
                    success: function (result) {
                        if (result.state === 0) {
                            layer.msg(result.msg, {time: 600, icon: 6}, function () {
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        } else {
                            layer.msg(result.msg, {time: 600, icon: 5});
                        }
                    }
                });
                return false;
            });
        });
    </script>

@endsection




